package com.cyc.basic.view;

import java.util.Arrays;

/**
 * @Description:
 * @version 1.0
 * @author cyc
 * @date 2021/9/30 10:14
 */
public class test {

    public static void main(String[] args) {
        int arr[] = new int[]{1, 2, 3, 4, 4, 3, 2};
        int[] maxArr = sercharMax(arr);
        System.out.println(Arrays.toString(maxArr));
    }

    public static int search(int[] nums) {
        int left = 0, right = nums.length - 1;
        int[] arr = new int[nums.length];
        int max = nums[0];
        while (left <= right) {
            int mid = (left + right) / 2;//从中间开始
            if (nums[mid] < max) {
                left = mid + 1;
            } else if (nums[mid] > max) {
                right = mid - 1;
            }
        }
        return -1;
    }

    /**
     * 返回最大值数组下标
     * @param arr
     * @return
     */
    public static int[] sercharMax(int[] arr) {
        int m = arr.length;
        int begin = 0, end = m - 1;
        int max = begin + (end - begin) / 2;
        int maxArr[] = new int[arr.length];

        int i = 0;
        while (max > 0 && max < m - 1) {
            //大于等于左边又大于等于右边, 那他就是最大值
            if (arr[max] >= arr[max + 1] && arr[max] >= arr[max - 1]) {
                maxArr[i] = arr[max];
                if ((arr[max] == arr[max + 1])) {
                    maxArr[i] = arr[max + 1];
                    for (int j = 0; j < arr.length; j++) {
                        if (max + 1 + j < arr.length - 1) {
                            if (maxArr[i + 1] == arr[max + 1 + j]) {
                                maxArr[i + 1 + j] = arr[max + 1 + j];
                                i++;
                            }
                        }
                    }
                } else if (arr[max] == arr[max - 1]) {
                    maxArr[i] = arr[max - 1];
                    for (int j = 0; j < arr.length; j++) {
                        if (max - 1 - j > 0) {
                            if (maxArr[i + 1] == arr[max + 1 - j]) {
                                maxArr[i + 1 + j] = arr[max + 1 - j];
                                i++;
                            }
                        }
                    }
                }
                return maxArr;
            } else if (arr[max] < arr[max + 1]) {
                //小于右边, 说明右边有大值, begin重新赋值
                begin = max + 1;
                max = begin + (end - begin) / 2;
            } else {
                //大于右边, 说明最大值在左边
                end = max - 1;
                max = begin + (end - begin) / 2;
            }
        }
        return maxArr;
    }
}

